<?php
class kwitansipenjualan_model extends CI_Model
{
	private $NoKwitansi;
    private $NoFaktur;
    private $namapelanggan;
    private $KodeBarang;
  	private $TotalQuantity;
	private $Discount;
	private $TotalAmount;
    private $Typebayar;
    private $KodeBank;
	private $Banknamepelanggan;
    private $NoRekeningpelanggan;
    private $Keterangan;
    
    private $Quantity;
	private $Amount;
    
    private $Createdby;
    private $Createdon;
    private $Lastupdateby;
  	private $Lastupdateon;
  	private $Active;
	
	function setNoKwitansi($nokwitansi){
		$this->NoKwitansi = $nokwitansi;
	}
	function getNoKwitansi(){
		return $this->NoKwitansi;
	}
	
	function setNoFaktur($nofaktur){
		$this->NoFaktur = $nofaktur;
	}
	function getNoFaktur(){
		return $this->NoFaktur;
	}
	
	function setnamapelanggan($namapelanggan){
		$this->namapelanggan = $namapelanggan;
	}
	function getnamapelanggan(){
		return $this->namapelanggan;
	}
	
	function setKodeBarang($kdbarang){
		$this->KodeBarang = $kdbarang;
	}
	function getKodeBarang(){
		return $this->KodeBarang;
	}
	
	function setTotalQuantity($ttlqty){
		$this->TotalQuantity = $ttlqty;
	}
	function getTotalQuantity(){
		return $this->TotalQuantity;
	}
	
	function setDiscount($discount){
		$this->Discount = $discount;
	}
	function getDiscount(){
		return $this->Discount;
	}
	
	function setTotalAmount($ttlamount){
		$this->TotalAmount = $ttlamount;
	}
	function getTotalAmount(){
		return $this->TotalAmount;
	}
	
	function setTypebayar($tipebyr){
		$this->Typebayar = $tipebyr;
	}
	function getTypebayar(){
		return $this->Typebayar;
	}
	
	function setKodeBank($kdbank){
		$this->KodeBank = $kdbank;
	}
	function getKodeBank(){
		return $this->KodeBank;
	}
	
	function setBanknamepelanggan($Banknamepelanggan){
		$this->Banknamepelanggan = $Banknamepelanggan;
	}
	function getBanknamepelanggan(){
		return $this->Banknamepelanggan;
	}
	
	function setNoRekeningpelanggan($NoRekeningpelanggan){
		$this->NoRekeningpelanggan = $NoRekeningpelanggan;
	}

	function getNoRekeningpelanggan(){
		return $this->NoRekeningpelanggan;
	}
	
	function setKeterangan($ket){
		$this->Keterangan = $ket;
	}
	function getKeterangan(){
		return $this->Keterangan;
	}
	
	function setQuantity($Quantity){
		$this->Quantity = $Quantity;
	}
	function getQuantity(){
		return $this->Quantity;
	}
	
	function setAmount($Amount){
		$this->Amount = $Amount;
	}
	function getAmount(){
		return $this->Amount;
	}
	
	function setCreatedBy($c_by){
		$this->CreatedBy = $c_by;
	}
	function getCreatedBy(){
		return $this->CreatedBy;
	}
	
	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->CreatedOn = $dateNow;
	}
	function getCreatedOn(){
		return $this->CreatedOn;
	}
	
	function setLastUpdateBy($u_by){
		$this->LastUpdateBy = $u_by;
	}
	function getLastUpdateBy(){
		return $this->LastUpdateBy;
	}

	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->LastUpdateOn = $dateNow;
	}
	function getLastUpdateOn(){
		return $this->LastUpdateOn;
	}

	function setActive($active_val){
		$this->Active = $active_val;
	}
	function getActive(){
		return $this->Active;
	}
	
	// Call the module constructor
	function __construct(){
		parent::__construct();
	}
	
	function getkwitansiCode()
	{
		$query = $this->db->query("select MAX(RIGHT(NoKwitansi,4)) as kdkwitansi from kwitansipenjualan");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idkwitansi)
			{
				$tmpkwitansi = ((int)$idkwitansi->kdkwitansi)+1;
				$kd = sprintf("%04s", $tmpkwitansi);
			}
			
		}
		else
		{
			$kd = "0001";
		}
		
		$kd = "KW".$kd; 
		
		return $kd;
	}
	
	function insert()
	{
		$data = array(
			'NoKwitansi' => $this->getNoKwitansi(),
			'NoFaktur' => $this->getNoFaktur(),
			'namapelanggan' =>$this->getnamapelanggan(),
			'TotalQuantity' => $this->getTotalQuantity(),
			'Discount' => $this->getDiscount(),
			'TotalAmount' => $this->getTotalAmount(),
			'Typebayar' => $this->getTypebayar(),
			'KodeBankpenerimaan' => $this->getKodeBank(),
			'Banknamepelanggan' => $this->getBanknamepelanggan(),
			'NoRekeningpelanggan' => $this->getNoRekeningpelanggan(),
			'Keterangan' => $this->getKeterangan(),
			'Createdby' => $this->getCreatedBy(),
			'Createdon' => $this->getCreatedOn(),
			'Lastupdateby' => $this->getLastupdateby(),
			'Lastupdateon' => $this->getLastupdateon(),
			'Active' => '1'						
		);
		$this->db->insert('kwitansipenjualan',$data);
	}
	
	function insertdetail()
	{
		$data1 = array(
			'NoKwitansi' => $this->getNoKwitansi(),
			'NoFaktur' => $this->getNoFaktur(),
			'Quantity' => $this->getQuantity(),
			'Discount' => '0',
			'Ppn' => '0',
			'Amount' => $this->getAmount()					
		);
		$this->db->insert('transaction_kwitansi',$data1);
	}
	
	function update(){
		
		$data = array(
			'NoFaktur' => $this->getNoFaktur(),
			'namapelanggan' =>$this->getnamapelanggan(),
			'TotalQuantity' => $this->getTotalQuantity(),
			'Discount' => '0',
			'TotalAmount' => $this->getTotalAmount(),
			'Typebayar' => $this->getTypebayar(),
			'KodeBankpenerimaan' => $this->getKodeBank(),
			'Banknamepelanggan' => $this->getBanknamepelanggan(),
			'NoRekeningpelanggan' => $this->getNoRekeningpelanggan(),
			'Keterangan' => $this->getKeterangan(),
			'Lastupdateby' => $this->getLastupdateby(),
			'Lastupdateon' => $this->getLastupdateon()		
		);
		$this->db->where('NoKwitansi',$this->getNoKwitansi());
		$this->db->update('kwitansipenjualan',$data);
	}
	
	
	function updatedetail()
	{
		$data1 = array(
			'NoKwitansi' => $this->getNoKwitansi(),
			'NoFaktur' => $this->getNoFaktur(),
			'Quantity' => $this->getQuantity(),
			'Discount' => $this->getDiscount(),
			'Ppn' => '0',
			'Amount' => $this->getAmount()					
		);
		$this->db->where(array('NoKwitansi' => $this->getNoKwitansi()));
        $this->db->update('transaction_kwitansi', $data);
	}
	
	function getlistnofaktur(){
		$this->db->select("subfaktur_product_penjualan.NoSubFaktur,subfaktur_product_penjualan.NoFaktur,
		faktur_product_penjualan.DateFaktur,subfaktur_product_penjualan.Namacustomer,subfaktur_product_penjualan.Quantity,
		subfaktur_product_penjualan.Amount,subfaktur_product_penjualan.Keterangan,
		detail_product.KodeDetailBarang,detail_product.Productname")
        ->join('faktur_product_penjualan', 'faktur_product_penjualan.NoFaktur=subfaktur_product_penjualan.NoFaktur')
		->join('transaction_faktur_product_penjualan', 'transaction_faktur_product_penjualan.NoFaktur=faktur_product_penjualan.NoFaktur')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_faktur_product_penjualan.KodeBarang')
		->where(array('subfaktur_product_penjualan.NoFaktur' => $this->getNoFaktur(),'subfaktur_product_penjualan.Active'=>'1'))
		->group_by(array('faktur_product_penjualan.NoFaktur','transaction_faktur_product_penjualan.KodeBarang'));
        $query = $this->db->get('subfaktur_product_penjualan');
        return $query->result();
	}
	
	function getlistnofakturlike(){
		$this->db->select("subfaktur_product_penjualan.NoSubFaktur,subfaktur_product_penjualan.NoFaktur,
		faktur_product_penjualan.DateFaktur,subfaktur_product_penjualan.Namacustomer,subfaktur_product_penjualan.Quantity,
		subfaktur_product_penjualan.Amount,subfaktur_product_penjualan.Keterangan,
		detail_product.KodeDetailBarang,detail_product.Productname")
        ->join('faktur_product_penjualan', 'faktur_product_penjualan.NoFaktur=subfaktur_product_penjualan.NoFaktur')
		->join('transaction_faktur_product_penjualan', 'transaction_faktur_product_penjualan.NoFaktur=faktur_product_penjualan.NoFaktur')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_faktur_product_penjualan.KodeBarang')
		->where(array('subfaktur_product_penjualan.Active'=>'1'))->like(array('subfaktur_product_penjualan.NoFaktur' => $this->getNoFaktur()))
		->group_by(array('faktur_product_penjualan.NoFaktur','transaction_faktur_product_penjualan.KodeBarang'));
        $query = $this->db->get('subfaktur_product_penjualan');
        return $query->result();
	}
	
	function getList()
	{
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select("subfaktur_product_penjualan.NoSubFaktur,subfaktur_product_penjualan.NoFaktur,
		faktur_product_penjualan.DateFaktur,subfaktur_product_penjualan.Namacustomer,subfaktur_product_penjualan.Quantity,
		subfaktur_product_penjualan.Amount,subfaktur_product_penjualan.Keterangan,
		detail_product.KodeDetailBarang,detail_product.Productname")
        ->join('faktur_product_penjualan', 'faktur_product_penjualan.NoFaktur=subfaktur_product_penjualan.NoFaktur')
		->join('transaction_faktur_product_penjualan', 'transaction_faktur_product_penjualan.NoFaktur=faktur_product_penjualan.NoFaktur')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_faktur_product_penjualan.KodeBarang')
		->where(array('subfaktur_product_penjualan.Active'=>'1','subfaktur_product_penjualan.Datesubfaktur' => $tanggal))
		->group_by(array('faktur_product_penjualan.NoFaktur','transaction_faktur_product_penjualan.KodeBarang'));
        $query = $this->db->get('subfaktur_product_penjualan');
        return $query->result();
	}
	
	
	function getListfakturkwt(){
		$this->db->select("kwitansipenjualan.NoKwitansi,transaction_kwitansi.NoFaktur,
		transaction_faktur_product_penjualan.KodeBarang,detail_product.Productname,transaction_faktur_product_penjualan.HargaBarang,
		detail_product.Typeunit,transaction_kwitansi.Quantity,transaction_kwitansi.Discount,transaction_kwitansi.Amount")
		->join('kwitansipenjualan', 'kwitansipenjualan.NoKwitansi=transaction_kwitansi.NoKwitansi')
		->join('transaction_faktur_product_penjualan', 'transaction_faktur_product_penjualan.NoFaktur=transaction_kwitansi.NoFaktur')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_faktur_product_penjualan.KodeBarang')
		->where(array('transaction_kwitansi.NoKwitansi' => $this->getNoKwitansi()));
        $query = $this->db->get('transaction_kwitansi');
        return $query->result();
	}
	
	function ambilnokwitansi1() {
        $this->db->select()
       	->where(array('kwitansipenjualan.NoKwitansi' => $this->getNoKwitansi()));
        $query = $this->db->get('kwitansipenjualan');
        return $query->result();
    }
}
